#coding:utf-8
# requests模块发送post请求
'''
@Time : 2024/07/28 13:38
@Author : Walle
@Email : 114514@cancanneed.com
@File : post_requests.py
@Software: PyCharm
'''
import requests
import json
import time
class Baidu(object):
    def __init__(self,word):
        self.url = 'https://fanyi.baidu.com/ait/text/translate'
        self.headers = {
            'Accept':'text/event-stream',
            'Accept-Encoding':'gzip, deflate, br, zstd',
            'Accept-Language':'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6',
            'Acs-token':'1722063607423_1722145327927_c/eXwJKAbPn+tauxEv5oF4NggaQLbt6l7/B28z/SJyYoP0/DOhjkoARY7r1UJwK8bjNbcaCGrBLbPZLONszgP8echL2zPXpigUc6TpWhJQjTXBlzY3qVop2lsdogsl42YHuELKX/HdYeeej22hatW0DIaokAa9kokT5Cwz0PFaZ7pp4Og2vF3s8wVdeR/YvCvhk04Ij5dhjuYFr1OhYkryJXFkrQN/4EcjIcG4JhRznvsJjiG/+3Zk93P347gEOYlB/7lMFzEv8/4fM1TM8L7QpxU78v5ohCkFnMyH1SBuyl/hGSLzDRJigTMMXX6jle93OPMMOP2sclkVgDC4F1jzk1x5ndE+sl5suMhUn2e7ytSabcc9QwwQjbFuWguGc108nc5xawPyTylfD7fnbyGCAgpkvK333azGvhMVnDKU8++rup/Fw3sYt/qgMo0Nwo',
            'Connection':'keep-alive',
            'Content-length': '201',
            'Content-Type':'application/json',
            'Host':'fanyi.baidu.com',
            'Origin':'https://fanyi.baidu.com',
            'Referer':'https://fanyi.baidu.com/mtpe-individual/multimodal?query=%E5%8C%BF%E5%90%8D&lang=zh2en',
            'Sec-Ch-Ua':'"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
            'Sec-Ch-Ua-Mobile':'?0',
            'Sec-Ch-Ua-Platform':'Windows',
            'Sec-Fetch-Dest':'empty',
            'Sec-Fetch-Mode':'cors',
            'Sec-Fetch-Site':'same-origin',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
            'Cookie': 'BAIDUID_BFESS=3F1F4B6DAE70FC06B76C12F919F6136C:FG=1; __bid_n=18f4e7b093e4384d9ae1a0; BIDUPSID=3F1F4B6DAE70FC06B76C12F919F6136C; PSTM=1715212292; BAIDU_WISE_UID=wapp_1715669792554_883; MCITY=-340%3A352%3A; BDUSS=WxlMjZLQVBma2J1Tm1uVllPSXpWRkg5dENSQW9xY3UxaDladjBmV1drSkNmYnhtRVFBQUFBJCQAAAAAAAAAAAEAAACxHAikuuzhsNPEv81oZWFydAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAELwlGZC8JRmL; BDUSS_BFESS=WxlMjZLQVBma2J1Tm1uVllPSXpWRkg5dENSQW9xY3UxaDladjBmV1drSkNmYnhtRVFBQUFBJCQAAAAAAAAAAAEAAACxHAikuuzhsNPEv81oZWFydAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAELwlGZC8JRmL; ZFY=SsiSa4Za:AN:AV5nEcjyz7VeYa:AS8FeNBYTJ9AbcYw6pM:C; H_PS_PSSID=60275_60360_60467_60492_60499_60523; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; ab_sr=1.0.1_ZWVmYjE2MDliOWViYmM4ODlkMTFiMDkwMmM3MTZiM2ExOTg2ZTZmYzViNTgxMTAxZTE4NDU2OTJhMjEzYmFkOTc5YTZiMTU2NDRmMTQwMDFhODc3MTkwMDA0MDJjOWNhZWFmMTE3ZGZiNGVmYjZkMTY1NmQ4YjhiZjBlYjZlMzFiNGVmMDIyODQyZTdjZTE0OGNkOGNhNzUyOTk3NmIyNA==; RT="z=1&dm=baidu.com&si=0e39907d-e404-423b-847c-01cd56b5b185&ss=lz51l28z&sl=9&tt=9x2&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=2hj67"'
        }
        self.timestamp = int(round(time.time() * 1000))
        self.word = word
        self.data = {
            "query": self.word,
            "from": "zh",
            "to": "en",
            "reference": "",
            "corpusIds": [],
            "qcSettings": [
                "1",
                "2",
                "3",
                "4",
                "5",
                "6",
                "7",
                "8",
                "9",
                "10",
                "11"
            ],
            "needPhonetic": False,
            "domain": "common",
            "milliTimestamp": self.timestamp,
        }

    def get_data(self):
        response = requests.post(self.url, headers=self.headers, data=self.data)
        return response.content.decode('utf-8')

    def parse_data(self):
        # data = json.loads(self.get_data())
        # return data
        pass


    def run(self):
        # 编写爬虫逻辑
        # url
        # headers
        # data字典
        # 发送请求获取响应
        res = self.get_data()
        return res

        # 数据解析



if __name__ == '__main__':
    baidu = Baidu('你好')
    ret = baidu.run()
    print(ret)





